package com.ysd.modules.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ysd.common.exception.RRException;
import com.ysd.common.utils.R;
import com.ysd.modules.sys.entity.TopicType;
import com.ysd.modules.sys.model.StatusReqs;
import com.ysd.modules.sys.service.TopicTypeService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dapeng
 * @since 2021-07-15
 */
@RestController
@RequestMapping("/topic/type")
public class TopicTypeController {
    @Autowired
    private TopicTypeService topicTypeService;

    /**
     * 列表查询
     * @param req
     * @return R
     */
    @GetMapping("/list")
    @RequiresPermissions("topic:type:list")
    public R listSearch(@RequestParam Map<String, Object> req){
        IPage<TopicType> page = topicTypeService.listSearch(req);
        R ok = R.ok();
        ok.put("data", page.getRecords());
        ok.put("count", page.getTotal());
        return ok;
    }

    /**
     * 查询详情
     * @param id
     * @return R
     */
    @GetMapping("/detail/search")
    @RequiresPermissions("topic:type:detail:search")
    public R listSearch(@RequestParam Integer id){
        TopicType topicType = topicTypeService.getById(id);
        return R.ok().put("data", topicType);
    }

    /**
     * 新增/修改
     * @param req
     * @return R
     */
    @PostMapping("/save")
    @RequiresPermissions("topic:type:save")
    public R save(@RequestBody TopicType req){
        this.verifyForm(req);
        if (req.getId() == null || 0 == req.getId()) {
            topicTypeService.save(req);
        } else {
            topicTypeService.updateById(req);
        }
        return R.ok();
    }

    /**
     * 批量修改状态
     * @param req
     * @return R
     */
    @PostMapping("/status/modify")
    @RequiresPermissions("topic:type:status:modify")
    public R statusModify(@RequestBody StatusReqs req){
        topicTypeService.statusModify(req.getReq());
        return R.ok();
    }

    /**
     * 校验数据
     * @param topicType
     * @return
     */
    public void verifyForm(TopicType topicType) {
        if(StringUtils.isBlank(topicType.getName())){
            throw new RRException("章节名称不能为空");
        }
    }
}

